﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Suspiros_ObjetosNegocio;
using Suspiros_Persistencia;

namespace SuspirosWeb
{
    public partial class Formulario_web114 : System.Web.UI.Page
    {

        OrdenDAO ordenes = new OrdenDAO();
        Orden orden = new Orden();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                conectaControl1("id");
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            conectaControl1("id");
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            conectaControl1("id");
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            conectaControl1("id");
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            //recupera renglon que haya sido modificado, y lo actualiza
            orden.Id = (int)GridView1.DataKeys[e.RowIndex].Value;
            orden.FechaCompra = Convert.ToDateTime(((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text);
            orden.FechaPago = Convert.ToDateTime(((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text);
            orden.Pago = double.Parse(((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text);
            orden.StatusOrden = Int32.Parse(((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text);

            ordenes.actualizarOrden(orden);
            GridView1.EditIndex = -1;
            conectaControl1("id");
        }

        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            conectaControl1(e.SortExpression.ToString());
        }

        private void conectaControl1(string campo)
        {
            GridView1.DataSource = ordenes.obtenerOrdenes(campo);
            GridView1.DataBind();
        }
    }
}